#include "log.h"
#include "IDocument.h"
#include "ILayoutAwareObject.h"
#include "ILayoutObjectCollection.h"


void WebAggregator::dataGrabber::IDocument::initialize()
{
	FILE_LOG(logDEBUG1) << "Entry: WebAggregator::dataGrabber::IDocument::initialize()";


	FILE_LOG(logDEBUG1) << "Exit: WebAggregator::dataGrabber::IDocument::initialize()";
}

void WebAggregator::dataGrabber::IDocument::unInitialize()
{
	FILE_LOG(logDEBUG1) << "Entry: WebAggregator::dataGrabber::IDocument::unInitialize()";


	FILE_LOG(logDEBUG1) << "Exit: WebAggregator::dataGrabber::IDocument::unInitialize()";
}

void WebAggregator::dataGrabber::IDocument::load(WebKit::IDOMDocument* document)
{
	FILE_LOG(logDEBUG1) << "Entry: void WebAggregator::dataGrabber::IDocument::load(WebKit::IDOMDocument* document)";

	doc = document;

	FILE_LOG(logDEBUG1) << "Exit: void WebAggregator::dataGrabber::IDocument::load(WebKit::IDOMDocument* document)";
}

std::string WebAggregator::dataGrabber::IDocument::Trim(std::string str)
{
	FILE_LOG(logDEBUG1) << "Entry: std::string WebAggregator::dataGrabber::IDocument::Trim(std::string str)";

	if(str.length() == 0)
		return str;

	int pos1 = 0;
	for(int i = 0; i < str.length(); i++)
		if((int)str.at(i) != 0)
		{
			pos1 = i;
			break;
		}

	int pos2 = 0;
	for(int j = str.length() - 1; j >= 0 ; j--)
		if((int)str.at(j) != 0)
		{
			pos2 = j;
			break;
		}

	if(pos1 == pos2)
		return "";

	FILE_LOG(logDEBUG1) << "Exit: std::string WebAggregator::dataGrabber::IDocument::Trim(std::string str)";

	return str.substr(pos1, pos2);
}

void WebAggregator::dataGrabber::IDocument::RecursiveTreeTraversal(WebAggregator::dataGrabber::ILayoutObjectCollection* col, WebKit::IDOMNodePtr const & node)
{
	FILE_LOG(logDEBUG1) << "Entry: void WebAggregator::dataGrabber::IDocument::RecursiveTreeTraversal(WebAggregator::dataGrabber::ILayoutObjectCollection* col, WebKit::IDOMNodePtr const & node)";

	if(node)
	{
		std::string textContent = Trim((char*)node->textContent());
		if(node->hasChildNodes() == 0)
		{
			ILayoutAwareObject* obj = NULL;
			obj = new ILayoutAwareObject(col);
			obj->setPosition(0, count++);
			obj->setAttribute("", textContent);
			col->layoutAwareObjects.insert(col->layoutAwareObjects.end(), obj);
		}
	
		int len = node->childNodes()->length();
		for (int i = 0; i < len; i++)
		{
				RecursiveTreeTraversal(col, node->childNodes()->item(i));
		}
	}

	FILE_LOG(logDEBUG1) << "Exit: void WebAggregator::dataGrabber::IDocument::RecursiveTreeTraversal(WebAggregator::dataGrabber::ILayoutObjectCollection* col, WebKit::IDOMNodePtr const & node)";
}

WebAggregator::dataGrabber::ILayoutObjectCollection* WebAggregator::dataGrabber::IDocument::getElements()
{
	FILE_LOG(logDEBUG1) << "Entry: WebAggregator::dataGrabber::ILayoutObjectCollection* WebAggregator::dataGrabber::IDocument::getElements()";

	WebAggregator::dataGrabber::ILayoutObjectCollection* col = new WebAggregator::dataGrabber::ILayoutObjectCollection();
	
	count = 0;
	int len = doc->documentElement()->childNodes()->length();
	for (int i = 0; i < len; i++)
	{
		RecursiveTreeTraversal(col, doc->documentElement()->childNodes()->item(i));
	}

    FILE_LOG(logDEBUG1) << "Exit: WebAggregator::dataGrabber::ILayoutObjectCollection* WebAggregator::dataGrabber::IDocument::getElements()";

	return col;
}